package com.doit.day01;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * 生产者
 */
public class ProducerDemo {
    public static void main(String[] args) throws InterruptedException {
        Properties props = new Properties();
        //需要设置什么咱现在也不懂。。。
        //这三个参数是创建生产者必须得配置的
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "linux01:9092");

        //选配的  kafka生产者的应答机制
        props.setProperty(ProducerConfig.ACKS_CONFIG,"-1");
//        props.setProperty("log.message.timestamp.type","LogAppendTime");

        //创建一个生产者对象
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);

        //通过对象去调用api 发送数据
        //封装发送的数据，并且指定发到哪个topic中
        for (int i = 0; i < 20; i++) {
            ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>("d43","this message from idea"+i);
            //发送数据
            producer.send(producerRecord);
            Thread.sleep(50);
        }
        producer.flush();
        //关闭资源
        producer.close();


    }
}
